<template>
  <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"label-width="68px">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入用户昵称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="用户状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList">
          <el-table-column label="用户ID" align="center" prop="userId"  />
          <el-table-column label="用户昵称" align="center" prop="sysUser.nickName"  :show-overflow-tooltip="true" />
          <el-table-column label="用户头像" align="center" prop="sysUser.nickName">
            <template slot-scope="scope">
              <ImagePreview :src="scope.row.sysUser.avatar" :width="40"></ImagePreview>
            </template>
          </el-table-column>
          <el-table-column label="手机号" align="center" prop="sysUser.phonenumber"/>
          <el-table-column label="英文名" align="center" prop="englishName"/>
          <el-table-column label="年龄" align="center" prop="age"/>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.sysUser.status"
                active-value="0"
                inactive-value="1"
                v-hasPermi="['system:user:edit']"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="注册时间" align="center" prop="createTime">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
  </div>
</template>

<script>
import { changeUserStatus } from "@/api/system/user";
import { listUser } from "@/api/spoken/user";

export default {
  name: "spokenUser",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      dateRange: [],
      userList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        phonenumber: undefined,
        status: undefined,
      },
    };
  },
 
  created() {
    this.getList();
   
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.sysUser.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.sysUser.nickName + '"用户吗？').then(function() {
        return changeUserStatus(row.userId, row.sysUser.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function(err) {
        console.log(err)
        row.sysUser.status = row.sysUser.status === "0" ? "1" : "0";
        console.log( row.sysUser.status)
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>